<?php
/**
 * 支付宝 支付
 * @author xjq
 * 2018年11月19日
 */
class ShouqianbaPay extends MY_Controller
{
	function __construct()
    {
        parent::__construct();
        
    }

    /**
    * 支付
    */
    function index(){
        $this->load->view('index');
    }

    /**
    * 支付宝支付
    */
    function AliPay(){
    	//加载 收钱吧 接口模型
        $this->load->library('Shouqianba');
        $pay = new Shouqianba();

        $ordersn=date('YmdHis').rand(1000,9999);
        $goodsName='测试商品';
        $orderprice='1';
        $url = 'https://api.shouqianba.com/upay/v2/precreate';
        //请求数据
        $post_data = array(
            'terminal_sn'       =>      '100005270005816389',
            'client_sn'         =>      $ordersn,
            'total_amount'      =>      $orderprice,
            'payway'            =>      '1',
            'subject'           =>      $goodsName,
            'operator'          =>      'xjq',
            'goods_details'     =>      array(array(
                                            'goods_id'      =>      '1',
                                            'goods_name'    =>      $goodsName,
                                            'quantity'      =>      '1',
                                            'price'         =>      $orderprice,
                                            'promotion_type'=>      '0'
                                            ))
                                        
            );
        //设置请求头
        $header = array('Content-Type: application/json',$pay->signHeader($post_data,1));
        $result = $pay->_post($url,json_encode($post_data),$header);
        if ($result['result_code'] == '200')
        {
            $url = $result['biz_response']['data']['qr_code_image_url'];

            $url_array = explode('?', $url);

            $url_array = explode('=', $url_array[1]);

            redirect(urldecode($url_array[1]));
        }else{
            echo '下单失败';
        }
    }

    function finishAliPay(){
        echo '支付成功！';
    }

    function backChangeAliPay(){
        echo "SUCCESS";
    }

    /**
    * 微信支付
    */
    function WxPay(){
        //加载 收钱吧 接口模型
        $this->load->library('Shouqianba');
        $pay = new Shouqianba();

        //微信回调函数
        $callback_url=base_url()."ShouqianbaPay/backChangeWxPay";
        //付款后 跳转地址
        $link=base_url().'finishWxPay';
        $ordersn=date('YmdHis').rand(1000,9999);
        $goodsName='测试商品';
        $orderprice='1';
        $data = array(
            'client_sn'    => isset($ordersn)?$ordersn:'',
            'notify_url'   => $callback_url,    // 支付回调地址
            'return_url'   => $link,    // 支付结束回显地址,不支持vue地址
            'subject'      => isset($goodsName)?$goodsName:'',  // 商品名
            'total_amount' => $orderprice,      // 价格，分为单位
        );

        $pay->wxPay($data);
    }

    function finishWxPay(){
        echo '支付成功！';
    }

    function backChangeWxPay(){
        echo "SUCCESS";
    }

}